<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>离线消息统计</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .stats-grid {
            display: flex;
            flex-direction: column;
            gap: 15px;
            margin-top: 20px;
        }
        .stat-card {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            border-left: 4px solid #007AFF;
        }
        .stat-card h3 {
            margin: 0 0 10px 0;
            color: #333;
        }
        .stat-card .count {
            font-size: 24px;
            font-weight: bold;
            color: #007AFF;
        }
        .refresh-btn {
            background-color: #007AFF;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        .refresh-btn:hover {
            background-color: #0056b3;
        }
        .no-messages {
            text-align: center;
            color: #666;
            font-style: italic;
            padding: 40px;
        }
        .badge {
            display: inline-block;
            min-width: 22px;
            padding: 2px 7px;
            font-size: 14px;
            font-weight: bold;
            color: #fff;
            background: #ff2d2d;
            border-radius: 12px;
            vertical-align: middle;
            margin-left: 8px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>离线消息统计</h1>
        <p>显示所有用户的离线消息数量</p>
        
        <button class="refresh-btn" onclick="loadStats()">刷新统计</button>
        
        <div id="statsContainer" class="stats-grid">
            <div class="no-messages">加载中...</div>
        </div>
    </div>

    <script>
        async function loadStats() {
            try {
                const response = await fetch('/offline-messages');
                const stats = await response.json();
                
                const container = document.getElementById('statsContainer');
                
                if (Object.keys(stats).length === 0) {
                    container.innerHTML = '<div class="no-messages">暂无离线消息</div>';
                    return;
                }
                
                container.innerHTML = '';
                
                const entries = Object.entries(stats);
                // 先按未读消息数量降序排列，未读为0的排最后
                entries.sort((a, b) => {
                    const countA = Number(a[1]);
                    const countB = Number(b[1]);
                    if (countA === 0 && countB > 0) return 1;
                    if (countA > 0 && countB === 0) return -1;
                    return countB - countA;
                });
                
                entries.forEach(([userId, count]) => {
                    const card = document.createElement('div');
                    card.className = 'stat-card';
                    card.innerHTML = `
                        <h3>用户: ${userId} 
                            ${count > 0 ? `<span class="badge">${count}</span>` : ''}
                        </h3>
                        <div class="count">${count} 条消息</div>
                    `;
                    container.appendChild(card);
                });
                
            } catch (error) {
                console.error('加载统计失败:', error);
                document.getElementById('statsContainer').innerHTML = 
                    '<div class="no-messages">加载失败，请检查服务器是否运行</div>';
            }
        }
        
        // 页面加载时自动刷新
        loadStats();
        
        // 每30秒自动刷新一次
        setInterval(loadStats, 30000);
    </script>
</body>
</html> 